package com.yinhe.music.yhmusic.songmenu;

import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.yinhe.music.common.utils.DisplayUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.MenuManagerAdapter;
import com.yinhe.music.yhmusic.application.MusicApplication;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuManagerContract;
import com.yinhe.music.yhmusic.songmenu.presenter.SongMenuManagerPresenter;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.MyItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

/**
 * 歌单管理
 *
 * @author hasee
 * @date 2017/8/24
 */

public class MenuManagerActivity extends BaseServiceActivity implements ISongMenuManagerContract.ISongMenuManagerView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerview;
    @BindView(R.id.delete)
    TextView delete;
    @BindView(R.id.play)
    TextView play;
    @BindView(R.id.add_to)
    TextView addTo;
    @BindView(R.id.down)
    TextView down;

    private MenuManagerAdapter mAdapter;
    private int menuType;
    private SongMenuManagerPresenter mPresenter;

    @Override
    public void initView() {
        if (getIntent() != null) {
            menuType = getIntent().getIntExtra("menuType", 0);
        }
        showQuickControl(false);
        setToolbar("批量操作");
        initBottom();
        mPresenter.getMenuList();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_manage;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SongMenuManagerPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    protected void onDestroy() {
        if (mAdapter != null) {
            mAdapter.setUnChek();
        }
        super.onDestroy();
    }

    private void initBottom() {
        play.setVisibility(View.GONE);
        down.setVisibility(View.GONE);
        addTo.setVisibility(View.GONE);
        delete.setOnClickListener(view -> setDeleteMenu());
    }

    @Override
    public void setSongMenuListUI(List<SongMenuList> songMenuList) {
        mAdapter = new MenuManagerAdapter(songMenuList);
        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this));
        recyclerview.setHasFixedSize(true);
        recyclerview.addItemDecoration(new MyItemDecoration(this, DividerItemDecoration.VERTICAL_LIST, -1,
                DisplayUtil.dp2px(MusicApplication.getContext(), 108)));
        recyclerview.setAdapter(mAdapter);
    }

    @Override
    public void updateSongMenuListUI(List<SongMenuList> deleteList) {
        List<SongMenuList> lists = mAdapter.getData();
        lists.removeAll(deleteList);
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 批量删除歌单点击事件
     */
    private void setDeleteMenu() {
        List<SongMenuList> lists = mAdapter.getData();
        mPresenter.reqDelete(lists, menuType);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_select, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_all) {
            mAdapter.selectAll(item);
        }
        return super.onOptionsItemSelected(item);
    }
}
